<?php

/**
 * 
 * @project     : innoTweets 1.0
 * @author      : Dino (DBK)
 * 
 * @package     : My Activity
 * @description : Recent activity of a user
 * 
 */
if (!defined('BASEPATH')) {
  exit('No direct script access allowed');
}

class Activity extends AUTH_Controller {

  public function index($user_name = NULL) {

    // Getting & Setting username
    $user_name = (empty($user_name) ? $this->current_user['user_name'] : $this->sanitize($user_name));

    // Checking username
    if (!empty($user_name)) {

      // Checking permission
      if (($user_name == $this->current_user['user_name']) || ($this->is_admin)) {

        // Getting the user
        $get_user_by_username = $this->user_model->get_user_by_username($user_name);

        // Checking user data
        if ($get_user_by_username['status'] == 1 && !empty($get_user_by_username['result'])) {

          // Getting activities of the user
          $get_my_tweets = $this->tweet_model->get_my_tweets($get_user_by_username['result']['user_id'], 25);
          $user_tweets = $get_my_tweets['result'];

          $get_my_replies = $this->tweet_model->get_my_replies($get_user_by_username['result']['user_id'], 25);
          $user_replies = $get_my_replies['result'];

          $get_my_events = $this->event_model->get_my_events($get_user_by_username['result']['user_id'], 25);
          $user_events = $get_my_events['result'];

          $get_my_albums = $this->album_model->get_albums($get_user_by_username['result']['user_id'], 25);
          $user_albums = $get_my_albums['result'];

          $get_my_like = $this->like_model->get_my_like($get_user_by_username['result']['user_id'], 25);
          $user_likes = $get_my_like['result'];

          $get_wall_posts = $this->wall_model->get_wall_post_by_me($get_user_by_username['result']['user_id'], 25);
          $wall_posts = $get_wall_posts['result'];

          $get_polls = $this->poll_model->get_my_polls(25, $get_user_by_username['result']['user_id']);
          $user_polls = $get_polls['result'];

          $get_my_poll_replies = $this->poll_model->get_my_poll_replies($get_user_by_username['result']['user_id'], 25);
          $user_poll_replies = $get_my_poll_replies['result'];

          $get_my_comments = $this->comment_model->get_my_comments($get_user_by_username['result']['user_id'], 25);
          $user_comments = $get_my_comments['result'];

          // Preparing activity data
          $activity_data = array();

          if (!empty($user_tweets) && is_array($user_tweets)) {
            foreach ($user_tweets as $tweet) {
              $activity_data[] = array('activity_type' => 1, 'activity_id' => $tweet['tweet_id'], 'activity_date' => $tweet['tweet_date'], 'activity_message' => strip_tags($tweet['tweet_subject']));
            }
          }

          if (!empty($user_replies) && is_array($user_replies)) {
            foreach ($user_replies as $reply) {
              $activity_data[] = array('activity_type' => 2, 'activity_id' => $reply['tweet_reply_id'], 'activity_date' => $reply['tweet_reply_date'], 'activity_message' => strip_tags(preg_replace('/(<br>)+$/', '', $reply['tweet_reply_message'])));
            }
          }

          if (!empty($user_events) && is_array($user_events)) {
            foreach ($user_events as $event) {
              $activity_data[] = array('activity_type' => 3, 'activity_id' => $event['event_id'], 'activity_date' => $event['event_date'], 'activity_message' => strip_tags($event['event_message']));
            }
          }

          if (!empty($user_albums) && is_array($user_albums)) {
            foreach ($user_albums as $album) {
              $activity_data[] = array('activity_type' => 4, 'activity_id' => $album['album_id'], 'activity_date' => $album['album_date'], 'activity_message' => strip_tags($album['album_name']));
            }
          }

          if (!empty($user_likes) && is_array($user_likes)) {
            foreach ($user_likes as $like) {
              $activity_data[] = array('activity_type' => 5, 'activity_id' => $like['like_type_id'], 'activity_date' => $like['like_date'], 'activity_message' => $like['like_type']);
            }
          }

          if (!empty($wall_posts) && is_array($wall_posts)) {
            foreach ($wall_posts as $wall_post) {
              $activity_data[] = array('activity_type' => 6, 'activity_id' => $wall_post['wall_post_id'], 'activity_date' => $wall_post['wall_post_date'], 'activity_message' => strip_tags(preg_replace('/(<br>)+$/', '', $wall_post['wall_post_message'])));
            }
          }

          if (!empty($user_polls) && is_array($user_polls)) {
            foreach ($user_polls as $user_poll) {
              $activity_data[] = array('activity_type' => 7, 'activity_id' => $user_poll['poll_id'], 'activity_date' => $user_poll['poll_date'], 'activity_message' => strip_tags(preg_replace('/(<br>)+$/', '', $user_poll['poll_message'])));
            }
          }

          if (!empty($user_poll_replies) && is_array($user_poll_replies)) {
            foreach ($user_poll_replies as $user_poll_reply) {
              $activity_data[] = array('activity_type' => 8, 'activity_id' => $user_poll_reply['poll_id'], 'activity_date' => $user_poll_reply['poll_reply_date'], 'activity_message' => strip_tags(preg_replace('/(<br>)+$/', '', $user_poll_reply['poll_message'])));
            }
          }

          if (!empty($user_comments) && is_array($user_comments)) {
            foreach ($user_comments as $user_comment) {
              $activity_data[] = array('activity_type' => 9, 'activity_id' => $user_comment['comment_type_id'], 'activity_date' => $user_comment['comment_date'], 'activity_message' => $user_comment['comment_type']);
            }
          }


          // Sorting & Limiting activity data (Last 25)
          @usort($activity_data, array('activity', 'date_compare'));
          $activity_data = @array_slice($activity_data, 0, 25);

          // Preparing and Loading view
          $view_data = array();
          $view_data['menu_active'] = 'users';
          $view_data['user_data'] = $get_user_by_username['result'];
          $view_data['activity_data'] = $activity_data;
          $this->load_view('activity', $view_data);
          return;
        }
      }
    }

    // Redirect if something wrong
    redirect('profile');
    return;
  }

  /**
   * Fuction to sort according to date
   * 
   */
  private function date_compare($a, $b) {
    $t1 = strtotime(date('Ymd', strtotime($a['activity_date'])));
    $t2 = strtotime(date('Ymd', strtotime($b['activity_date'])));
    return $t2 - $t1;
  }

}

?>